
import store from '@/store'
import share from '@/shop/share'
import http from '@/config/requestConfig'
import $platform from '@/shop/platform'
import {
	router
} from '@/router'
export default {
	onLoad(options) {
		let that = this;
		
		// #ifdef MP
		if (options?.scene) {
			let scene = decodeURIComponent(options.scene);
			let sceneObj = scene.split('=');
			options[sceneObj[0]] = sceneObj[1];
		}
		// 如果有mid表示是推荐人的id
		if(options?.shareid){
			uni.setStorageSync("shareid", options.shareid);
			uni.setStorageSync("mid", options.shareid);
		}
		// #endif
		if (options?.page) {
			router.push({
				path: decodeURIComponent(options.page)
			})
		}

		if (options?.spm) {
			let shareParams = share.getShareQuery(options.spm);
			
			if (shareParams.shareUserId) {
				if (store.getters.isLogin) {
					http('common.shareAdd', {
						spm: options.spm
					});
				} else {
					uni.setStorageSync('spm', options.spm);
				}
			}
			
			let page = {};
			if (shareParams.page) {
				page.path = shareParams.page;
			}
			if (shareParams.pageId) {
				page.query = {
					id: shareParams.pageId
				}
			}
			if (page.path) {
				router.push(page);
			}
		}
	},
	// #ifdef MP-WEIXIN
	// 分享好友
	onShareAppMessage(res) {
		
		let that = this;
		// uni.$emit('ON_WX_SHARE')
		let shareInfo = store.getters.shareInfo;
		// 分享用户的mid
		shareInfo.path += '?shareid=' +  store.state.userInfo.uid;
		// 团主列表页面
		if(this.$Route.path === '/pages/prefile/group-leader/group-leader' && res.target.dataset.detail.id){
			uni.showToast({
				icon: "none",
				title: store.state.userInfo.uid
			})
			shareInfo.path = `/pages/index/goods-detail/goods-detail?id=${res.target.dataset.detail.id}&shareid=${store.state.userInfo.uid}`;
			shareInfo.image = res.target.dataset.detail.has_one_group.has_one_goods.thumb
			shareInfo.title = `[群主团]和我一起拼团，领${res.target.dataset.detail.subsidy}元商家补贴`
		}
		console.log(shareInfo.path);
		console.log(this.$Route);
		return {
			title: shareInfo.title,
			path: shareInfo.path,
			imageUrl: shareInfo.image,
			success(res) {
				uni.showToast({
					title: '分享成功'
				})
			},
			fail(res) {
				uni.showToast({
					title: '分享失败',
					icon: 'none'
				})
			},
			complete() {}
		}
	},
	// 分享到朋友圈
	// onShareTimeline(res) {
	// 	let that = this;
	// 	let shareInfo = store.getters.shareInfo;
	// 	let query = shareInfo.query;
	// 	//携带当前页面资源ID参数
	// 	let currentPage = getCurrentPages()[getCurrentPages().length - 1];
	// 	let options = currentPage.options;
	// 	if (JSON.stringify(options) != '{}' && options.id) {
	// 		query += `&id=${options.id}`;
	// 	}
	// 	return {
	// 		title: shareInfo.title,
	// 		query: query,
	// 		imageUrl: shareInfo.image,
	// 		success(res) {
	// 			uni.showToast({
	// 				title: '分享成功'
	// 			})
	// 		},
	// 		fail(res) {
	// 			uni.showToast({
	// 				title: '分享失败',
	// 				icon: 'none'
	// 			})
	// 		},
	// 		complete() {}
	// 	}
	// }
	// #endif
}
